package net.hvacapp.cmvrfforandroid.ui.mine;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mirror.android.common.util.StringUtils;

import net.hvacapp.cmvrfforandroid.AppManager;
import net.hvacapp.cmvrfforandroid.R;
import net.hvacapp.cmvrfforandroid.constant.Constant;
import net.hvacapp.cmvrfforandroid.http.BasicHttpClient;
import net.hvacapp.cmvrfforandroid.http.CommonResponse;
import net.hvacapp.cmvrfforandroid.http.HttpCallbackHandle;
import net.hvacapp.cmvrfforandroid.http.HttpConstant;
import net.hvacapp.cmvrfforandroid.manager.AccountManager;
import net.hvacapp.cmvrfforandroid.model.GetUserProfileDataResp;
import net.hvacapp.cmvrfforandroid.model.UserExistDataResp;
import net.hvacapp.cmvrfforandroid.model.UserProfileData;
import net.hvacapp.cmvrfforandroid.ui.BaseActivity;
import net.hvacapp.cmvrfforandroid.ui.selections.SelectionAdministatorActivity;
import net.hvacapp.cmvrfforandroid.ui.selections.SelectionBranchCompanyActivity;
import net.hvacapp.cmvrfforandroid.ui.selections.SelectionCityActivity;
import net.hvacapp.cmvrfforandroid.ui.selections.SelectionNationActivity;
import net.hvacapp.cmvrfforandroid.ui.selections.SelectionParentActivity;
import net.hvacapp.cmvrfforandroid.ui.selections.SelectionProvinceActivity;
import net.hvacapp.cmvrfforandroid.ui.selections.model.AreaData;
import net.hvacapp.cmvrfforandroid.utils.AnnotationUtil;
import net.hvacapp.cmvrfforandroid.utils.UIHelper;
import net.hvacapp.cmvrfforandroid.widget.FButton;
import net.hvacapp.cmvrfforandroid.widget.IOSAlertView;
import net.hvacapp.cmvrfforandroid.widget.UserSelectionView;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@EActivity(R.layout.activity_account_manager)
public class AccountManagerActivity extends BaseActivity {

    private final int Select_Nation = 1;
    private final int Select_Province = 2;
    private final int Select_City = 3;
    private final int Select_Administrator = 4;
    private final int Select_Branch_Administrator = 5;
    private final int Select_Parent = 6;
    private final int Select_Branch = 7;

    @ViewById(R.id.sel_userlevel)
    UserSelectionView mUserLevelSel;

    @ViewById(R.id.tv_email)
    TextView mEmailTv;
    @ViewById(R.id.cet_email)
    EditText mEmailCet;
    @ViewById(R.id.cet_password)
    EditText mPasswordCet;
    @ViewById(R.id.cet_confirm_password)
    EditText mConfirmPasswordCet;

    @ViewById(R.id.cet_name)
    EditText mNameCet;
    @ViewById(R.id.ll_contact_name)
    LinearLayout mContactNameLl;
    @ViewById(R.id.cet_contact_name)
    EditText mContactNameCet;
    @ViewById(R.id.cet_contact_number)
    EditText mContactNumberCet;

    @ViewById(R.id.ll_area_section)
    LinearLayout mAreaSectionLl;
    @ViewById(R.id.sel_nation)
    UserSelectionView mNationSel;
    @ViewById(R.id.sel_province)
    UserSelectionView mProvinceSel;
    @ViewById(R.id.sel_city)
    UserSelectionView mCitySel;

    @ViewById(R.id.sel_parent)
    UserSelectionView mParentSel;

    @ViewById(R.id.btn_register)
    FButton mRegister;

    private String mUserLevel;
    private String mEmail;

    private ArrayList<AreaData> mSelectedNation;
    private ArrayList<AreaData> mSelectedProvince;
    private ArrayList<AreaData> mSelectedCity;

    private ArrayList<UserProfileData> mSelectedAdministrator;
    private ArrayList<UserProfileData> mSelectedBranchAdministrator;
    private ArrayList<UserProfileData> mSelectedParent;
    private ArrayList<UserProfileData> mSelectedBranch;

    private Dialog mProgressDlg;
    private UserProfileData mUserProfileData;

    /**
     * 页面加载完后执行
     */
    @AfterViews
    @Override
    protected void viewDidLoad() {
        super.viewDidLoad();
        initEvent();
    }

    /**
     * 初始化
     */
    @Override
    public void initBoot() {
        setupToolbar(0, "账户管理", null);
    }

    /**
     * 初始化数据
     */
    @Override
    public void initData(Intent intent) {
        mUserLevelSel.setTitleString("用户级别");
        mUserLevelSel.hideSelectHint();
        mNationSel.setTitleString("修改经营国家");
        mProvinceSel.setTitleString("修改经营省份");
        mCitySel.setTitleString("修改经营城市");

        AreaData defaultNation = AppManager.getAppManager().getmDefaultNation();
        if (defaultNation != null) {
            mSelectedNation = new ArrayList<>();
            mSelectedNation.add(defaultNation);
            mNationSel.setInfoString(defaultNation.getName());
        }
//        setupViewsByUserLevel(1);

        loadUserProfileData();
    }

    private void loadUserProfileData() {
        BasicHttpClient.httpGetAsync(HttpConstant.HTTP_GET_USER_PROFILE, AccountManager.getInstance().getToken(), new HttpCallbackHandle<GetUserProfileDataResp>() {
            @Override
            public void onSuccess(GetUserProfileDataResp obj) {
                // 成功
                mUserProfileData = obj.getGetUserProfileResult();
                updateViews();
                setAreaInfo();
            }

            @Override
            public void onFailure(String response) {

            }

        });
    }


    private void updateViews() {
        if (mUserProfileData != null) {

            mUserLevel = mUserProfileData.getRole();
            if (StringUtils.isEquals(mUserProfileData.getRole(), Constant.UserTypeBranchCompany)) {
                mSelectedAdministrator = new ArrayList<>();
                UserProfileData data = new UserProfileData();
                data.setUserName(mUserProfileData.getParentCompanyUserName());
                data.setName(mUserProfileData.getParentCompany());
                mSelectedAdministrator.add(data);

                mUserLevelSel.setInfoString("暖通公司");
                mParentSel.setTitleString("修改所属大区管理员");
            } else if (StringUtils.isEquals(mUserProfileData.getRole(), Constant.UserTypeAdministrator)) {
                mUserLevelSel.setInfoString("管理员");
                mParentSel.setVisibility(View.GONE);
                mAreaSectionLl.setVisibility(View.GONE);
                mContactNameLl.setVisibility(View.GONE);
            } else if (StringUtils.isEquals(mUserProfileData.getRole(), Constant.UserTypeBranchCompanyAdmin)) {
                mSelectedBranch = new ArrayList<>();
                UserProfileData data = new UserProfileData();
                data.setUserName(mUserProfileData.getParentCompanyUserName());
                data.setName(mUserProfileData.getParentCompany());
                mSelectedBranch.add(data);

                mUserLevelSel.setInfoString("暖通公司零售经理");
                mParentSel.setTitleString("修改所属暖通公司");
                mContactNameLl.setVisibility(View.GONE);
            } else if (StringUtils.isEquals(mUserProfileData.getRole(), Constant.UserTypeAreaAdmin)) {
                mUserLevelSel.setInfoString("大区管理员");
                mParentSel.setVisibility(View.GONE);
//                mAreaSectionLl.setVisibility(View.GONE);
                mContactNameLl.setVisibility(View.GONE);
            } else if (StringUtils.isEquals(mUserProfileData.getRole(), Constant.UserTypeDealer)) {
                mSelectedBranchAdministrator = new ArrayList<>();
                UserProfileData data = new UserProfileData();
                data.setUserName(mUserProfileData.getParentCompanyUserName());
                data.setName(mUserProfileData.getParentCompany());
                mSelectedBranchAdministrator.add(data);

                mUserLevelSel.setInfoString("经销商");
                mParentSel.setTitleString("修改暖通公司零售经理");
            } else if (StringUtils.isEquals(mUserProfileData.getRole(), Constant.UserTypeSalePersonal)) {
                mSelectedParent = new ArrayList<>();
                UserProfileData data = new UserProfileData();
                data.setUserName(mUserProfileData.getParentCompanyUserName());
                data.setName(mUserProfileData.getParentCompany());
                mSelectedParent.add(data);

                mUserLevelSel.setInfoString("销售人员");
                mParentSel.setTitleString("修改暖通公司或者经销商");
                mContactNameLl.setVisibility(View.GONE);
            }
            else if (StringUtils.isEquals(mUserProfileData.getRole(), Constant.UserTypeCompany)) {
                mUserLevelSel.setInfoString("生产厂家");
                mParentSel.setVisibility(View.GONE);
                mAreaSectionLl.setVisibility(View.GONE);
                mContactNameLl.setVisibility(View.GONE);
            }


            mEmail = mUserProfileData.getUserName();
            mEmailCet.setText(mUserProfileData.getUserName());
            mEmailTv.setText(mUserProfileData.getUserName());
            if (!StringUtils.isEmpty(mUserProfileData.getName())) {
                mNameCet.setText(mUserProfileData.getName());
            }
            if (!StringUtils.isEmpty(mUserProfileData.getContactName())) {
                mContactNameCet.setText(mUserProfileData.getContactName());
            }
            if (!StringUtils.isEmpty(mUserProfileData.getPhoneNumber())) {
                mContactNumberCet.setText(mUserProfileData.getPhoneNumber());
            }
            if (!StringUtils.isEmpty(mUserProfileData.getParentCompany())) {
                mParentSel.setInfoString(mUserProfileData.getParentCompany());
            }

        }

    }

    private void setAreaInfo() {
        mNationSel.setInfoString(mUserProfileData.getOperationNationNames());
        mProvinceSel.setInfoString(mUserProfileData.getOperationProvinceNames());
        mCitySel.setInfoString(mUserProfileData.getOperationCityNames());

        if (!StringUtils.isEmpty(mUserProfileData.getOperationNations()) && !StringUtils.isEmpty(mUserProfileData.getOperationNationNames())) {
            mSelectedNation = new ArrayList<>();
            String id[] = mUserProfileData.getOperationNations().split(",");
            String name[] = mUserProfileData.getOperationNationNames().split(",");
            int count = id.length > name.length ? name.length : id.length;
            for (int i = 0; i < count; i++) {
                //验证数据合法性
                if (id[i].length() != Constant.EMPTY_DEVICE_ID.length()) {
                    continue;
                }
                AreaData data = new AreaData();
                data.setGuidId(id[i]);
                data.setName(name[i]);
                mSelectedNation.add(data);
            }
        }

        if (!StringUtils.isEmpty(mUserProfileData.getOperationProvinces()) && !StringUtils.isEmpty(mUserProfileData.getOperationProvinceNames())) {
            mSelectedProvince = new ArrayList<>();
            String id[] = mUserProfileData.getOperationProvinces().split(",");
            String name[] = mUserProfileData.getOperationProvinceNames().split(",");
            int count = id.length > name.length ? name.length : id.length;
            for (int i = 0; i < count; i++) {
                //验证数据合法性
                if (id[i].length() != Constant.EMPTY_DEVICE_ID.length()) {
                    continue;
                }
                AreaData data = new AreaData();
                data.setGuidId(id[i]);
                data.setName(name[i]);
                mSelectedProvince.add(data);
            }
        }

        if (!StringUtils.isEmpty(mUserProfileData.getOperationCities()) && !StringUtils.isEmpty(mUserProfileData.getOperationCityNames())) {
            mSelectedCity = new ArrayList<>();
            String id[] = mUserProfileData.getOperationCities().split(",");
            String name[] = mUserProfileData.getOperationCityNames().split(",");
            int count = id.length > name.length ? name.length : id.length;
            for (int i = 0; i < count; i++) {
                //验证数据合法性
                if (id[i].length() != Constant.EMPTY_DEVICE_ID.length()) {
                    continue;
                }
                AreaData data = new AreaData();
                data.setGuidId(id[i]);
                data.setName(name[i]);
                mSelectedCity.add(data);
            }
        }

    }


    /**
     * 页面名称
     */
    @Override
    public String getName() {
        return "";
    }

    private void initEvent() {
        mNationSel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(AccountManagerActivity.this, AnnotationUtil.get(SelectionNationActivity.class));
                Bundle bundle = new Bundle();
                bundle.putParcelableArrayList("nation", mSelectedNation);
                bundle.putBoolean("multiple", false);
                intent.putExtras(bundle);
                startActivityForResult(intent, Select_Nation);
            }
        });

        mProvinceSel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mSelectedNation == null || mSelectedNation.size() == 0) {
                    showMessage("请先选择国家！");
                }
                else {
                    Intent intent = new Intent(AccountManagerActivity.this, AnnotationUtil.get(SelectionProvinceActivity.class));
                    Bundle bundle = new Bundle();
                    bundle.putParcelableArrayList("nation", mSelectedNation);
                    bundle.putParcelableArrayList("province", mSelectedProvince);
                    bundle.putBoolean("multiple", true);
                    intent.putExtras(bundle);
                    startActivityForResult(intent, Select_Province);
                }
            }
        });
        mCitySel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mSelectedProvince == null || mSelectedProvince.size() == 0) {
                    showMessage("请先选择省份！");
                }
                else {
                    Intent intent = new Intent(AccountManagerActivity.this, AnnotationUtil.get(SelectionCityActivity.class));
                    Bundle bundle = new Bundle();
                    bundle.putParcelableArrayList("province", mSelectedProvince);
                    bundle.putParcelableArrayList("city", mSelectedCity);
                    bundle.putBoolean("multiple", true);
                    intent.putExtras(bundle);
                    startActivityForResult(intent, Select_City);
                }
            }
        });
        mParentSel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (StringUtils.isEquals(mUserLevel, Constant.UserTypeBranchCompany)) {
                    //选择大区管理员
                    Intent intent = new Intent(AccountManagerActivity.this, AnnotationUtil.get(SelectionAdministatorActivity.class));
                    Bundle bundle = new Bundle();
                    bundle.putBoolean("isBranch", false);
                    bundle.putParcelableArrayList("administrator", mSelectedAdministrator);
                    bundle.putBoolean("multiple", false);
                    intent.putExtras(bundle);
                    startActivityForResult(intent, Select_Administrator);
                } else if (StringUtils.isEquals(mUserLevel, Constant.UserTypeDealer)) {
                    //选择暖通公司零售经理
                    Intent intent = new Intent(AccountManagerActivity.this, AnnotationUtil.get(SelectionAdministatorActivity.class));
                    Bundle bundle = new Bundle();
                    bundle.putBoolean("isBranch", true);
                    bundle.putParcelableArrayList("administrator", mSelectedBranchAdministrator);
                    bundle.putBoolean("multiple", false);
                    intent.putExtras(bundle);
                    startActivityForResult(intent, Select_Branch_Administrator);
                } else if (StringUtils.isEquals(mUserLevel, Constant.UserTypeSalePersonal)) {
                    //选择暖通公司或者经销商
                    Intent intent = new Intent(AccountManagerActivity.this, AnnotationUtil.get(SelectionParentActivity.class));
                    Bundle bundle = new Bundle();
                    bundle.putParcelableArrayList("parent", mSelectedParent);
                    bundle.putBoolean("multiple", false);
                    intent.putExtras(bundle);
                    startActivityForResult(intent, Select_Parent);
                } else if (StringUtils.isEquals(mUserLevel, Constant.UserTypeBranchCompanyAdmin)) {
                    //选择暖通公司
                    Intent intent = new Intent(AccountManagerActivity.this, AnnotationUtil.get(SelectionBranchCompanyActivity.class));
                    Bundle bundle = new Bundle();
                    bundle.putParcelableArrayList("branch", mSelectedBranch);
                    bundle.putBoolean("multiple", false);
                    intent.putExtras(bundle);
                    startActivityForResult(intent, Select_Branch);
                }

            }
        });



        mEmailCet.setOnFocusChangeListener(new View.OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                EditText et = (EditText) v;
                if (!hasFocus && !StringUtils.isEquals(mEmail, et.getText().toString())) {
                    //检查输入的注册邮箱是否合法
                    if (UIHelper.isEmail(et.getText().toString())) {
                        mEmail = et.getText().toString();
                        isUserExist(mEmail);
                    } else {
                        showMessage("输入邮箱不合法，请重新输入！");
                    }
                }
            }
        });

        mConfirmPasswordCet.setOnFocusChangeListener(new View.OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    //检查输入的注册邮箱是否合法
                    EditText et = (EditText) v;
                    if (!StringUtils.isEquals(et.getText().toString(), mPasswordCet.getText().toString())) {
                        showMessage("两次输入密码不一致，请重新输入！");
                        et.setText("");
                        mPasswordCet.setText("");
                    }
                }
            }
        });

        mRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateUserInfo();
            }
        });
    }

    /**
     * 回调
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != this.RESULT_OK) {
            return;
        }
        if (requestCode == Select_Nation) {
            if (data != null) {
                mSelectedNation = data.getParcelableArrayListExtra("nation");
                String selStr = "";
                if (mSelectedNation != null && mSelectedNation.size() > 0) {
                    for (AreaData item: mSelectedNation) {
                        selStr += item.getName();
                        selStr += " ";
                    }
                }
                mNationSel.setInfoString(selStr);
            }
        }
        else if (requestCode == Select_Province) {
            if (data != null) {
                mSelectedProvince = data.getParcelableArrayListExtra("province");
                String selStr = "";
                if (mSelectedProvince != null && mSelectedProvince.size() > 0) {
                    for (AreaData item: mSelectedProvince) {
                        selStr += item.getName();
                        selStr += " ";
                    }
                }
                mProvinceSel.setInfoString(selStr);
            }
        }
        else if (requestCode == Select_City) {
            if (data != null) {
                mSelectedCity = data.getParcelableArrayListExtra("city");
                String selStr = "";
                if (mSelectedCity != null && mSelectedCity.size() > 0) {
                    for (AreaData item: mSelectedCity) {
                        selStr += item.getName();
                        selStr += " ";
                    }
                }
                mCitySel.setInfoString(selStr);
            }
        }
        else if (requestCode == Select_Administrator) {
            if (data != null) {
                mSelectedAdministrator = data.getParcelableArrayListExtra("administrator");
                String selStr = "";
                if (mSelectedAdministrator != null && mSelectedAdministrator.size() > 0) {
                    for (UserProfileData item: mSelectedAdministrator) {
                        selStr += item.getName();
                        selStr += " ";
                    }
                }
                mParentSel.setInfoString(selStr);
            }
        }
        else if (requestCode == Select_Branch_Administrator) {
            if (data != null) {
                mSelectedBranchAdministrator = data.getParcelableArrayListExtra("administrator");
                String selStr = "";
                if (mSelectedBranchAdministrator != null && mSelectedBranchAdministrator.size() > 0) {
                    for (UserProfileData item: mSelectedBranchAdministrator) {
                        selStr += item.getName();
                        selStr += " ";
                    }
                }
                mParentSel.setInfoString(selStr);
            }
        }
        else if (requestCode == Select_Parent) {
            if (data != null) {
                mSelectedParent = data.getParcelableArrayListExtra("parent");
                String selStr = "";
                if (mSelectedParent != null && mSelectedParent.size() > 0) {
                    if (mSelectedCity != null) {
                        mSelectedCity.clear();
                        mCitySel.setInfoString("");
                    }
                    for (UserProfileData item: mSelectedParent) {
                        selStr += item.getName();
                        selStr += " ";
                    }
                }
                mParentSel.setInfoString(selStr);
            }
        }
        else if (requestCode == Select_Branch) {
            if (data != null) {
                mSelectedBranch = data.getParcelableArrayListExtra("branch");
                String selStr = "";
                if (mSelectedBranch != null && mSelectedBranch.size() > 0) {
                    for (UserProfileData item: mSelectedBranch) {
                        selStr += item.getName();
                        selStr += " ";
                    }
                }
                mParentSel.setInfoString(selStr);
            }
        }
    }

    private void isUserExist(String userName) {
        Map<String, Object> param = new HashMap<>();
        param.put("un", new String(Base64.encode((userName).getBytes(), Base64.DEFAULT)));

        BasicHttpClient.httpGetAsync(HttpConstant.HTTP_GET_ISUSEREXIST, param, new HttpCallbackHandle<UserExistDataResp>() {
            @Override
            public void onSuccess(UserExistDataResp obj) {
                // 成功
                if (obj.getIsUserExistResult() == 1) {
                    //用户存在
                    showMessage("该用户名已经注册，请重新输入！");
                    mEmailCet.setText(mUserProfileData.getUserName());
                    mEmail = mUserProfileData.getUserName();
                }
                else {

                }
            }

            @Override
            public void onFailure(String response) {

            }
        });

    }

    private void updateUserInfo() {
        if (StringUtils.isEmpty(mEmail)) {
            showMessage("注册的用户名不能为空");
            return;
        }

        if (!StringUtils.isEmpty(mPasswordCet.getText().toString()) && mPasswordCet.getText().length() < 4) {
            showMessage("密码必须大于3位");
            return;
        }

        if (!StringUtils.isEquals(mPasswordCet.getText().toString(), mConfirmPasswordCet.getText().toString())) {
            showMessage("两次密码不一致");
            return;
        }

        if (StringUtils.isEquals(mUserLevel, Constant.UserTypeBranchCompanyAdmin) ||
                StringUtils.isEquals(mUserLevel, Constant.UserTypeAdministrator)) {

        }
        else {
            if (StringUtils.isEmpty(mContactNameCet.getText().toString())) {
                showMessage("请输入联系人姓名");
                return;
            }
        }


        if (StringUtils.isEmpty(mContactNumberCet.getText().toString())) {
            showMessage("请输入联系人手机号码");
            return;
        }

        if (StringUtils.isEquals(mUserLevel, Constant.UserTypeBranchCompany) ||
                StringUtils.isEquals(mUserLevel, Constant.UserTypeDealer)) {
            if (StringUtils.isEmpty(mNameCet.getText().toString())) {
                showMessage("请输入暖通公司名称");
                return;
            }
        }

        if (StringUtils.isEquals(mUserLevel, Constant.UserTypeBranchCompany) && mSelectedAdministrator != null && mSelectedAdministrator.size() > 0) {
            mUserProfileData.setParentCompany(mSelectedAdministrator.get(0).getName());
            mUserProfileData.setParentCompanyUserName(mSelectedAdministrator.get(0).getUserName());
        }
        else if (StringUtils.isEquals(mUserLevel, Constant.UserTypeDealer) && mSelectedBranchAdministrator != null && mSelectedBranchAdministrator.size() > 0) {
            mUserProfileData.setParentCompany(mSelectedBranchAdministrator.get(0).getName());
            mUserProfileData.setParentCompanyUserName(mSelectedBranchAdministrator.get(0).getUserName());
        }
        else if (StringUtils.isEquals(mUserLevel, Constant.UserTypeSalePersonal) && mSelectedParent != null && mSelectedParent.size() > 0) {
            mUserProfileData.setParentCompany(mSelectedParent.get(0).getName());
            mUserProfileData.setParentCompanyUserName(mSelectedParent.get(0).getUserName());
        }
        else if (StringUtils.isEquals(mUserLevel, Constant.UserTypeBranchCompanyAdmin) && mSelectedBranch != null && mSelectedBranch.size() > 0) {
            mUserProfileData.setParentCompany(mSelectedBranch.get(0).getName());
            mUserProfileData.setParentCompanyUserName(mSelectedBranch.get(0).getUserName());
        }

        mUserProfileData.setName(mNameCet.getText().toString());
        mUserProfileData.setContactName(mContactNameCet.getText().toString());
        mUserProfileData.setPhoneNumber(mContactNumberCet.getText().toString());

        //获得经营城市的字符串组合
        if (mSelectedCity != null && mSelectedCity.size() > 0) {
            StringBuilder cityStr = new StringBuilder();
            boolean isFirst = true;
            for (AreaData item:mSelectedCity) {
                if (isFirst) {
                    cityStr.append(item.getGuidId());
                    isFirst = false;
                }
                else {
                    cityStr.append(",");
                    cityStr.append(item.getGuidId());
                }
            }
            mUserProfileData.setOperationCities(cityStr.toString());
        }

        Map<String,Object> param = new HashMap<>();

        if (StringUtils.isEmpty(mPasswordCet.getText().toString())) {
            param.put("newPassword", null);
        }
        else {
            String password = new String(Base64.encode((mPasswordCet.getText().toString().trim()).getBytes(), Base64.DEFAULT));
            password = password.replaceAll("\r|\n", "");
            param.put("newPassword", password);
        }

        param.put("userProfile", mUserProfileData);

        if (mProgressDlg ==  null) {
            mProgressDlg = createProgressBarDialog(this, "修改用户信息中...");
        }
        mProgressDlg.show();

        BasicHttpClient.httpPostAsync(HttpConstant.HTTP_POST_USER_INFO, AccountManager.getInstance().getToken(), param, new HttpCallbackHandle<CommonResponse>() {
            @Override
            public void onSuccess(CommonResponse obj) {
                // 成功
                mProgressDlg.dismiss();
                IOSAlertView.showAlertView(AccountManagerActivity.this, "提示",
                        "保存用户信息成功！", "确定",
                        new IOSAlertView.OnAlertViewClickListener() {

                            @Override
                            public void OnAlertViewClick(int position) {
                                if (position == 0) {
                                    finish();
                                }
                            }
                        });

            }

            @Override
            public void onFailure(String response) {
                mProgressDlg.dismiss();
                showMessage("修改用户信息失败，请重试！");
            }
        });

    }



}
